//
//  NSError+MKAPIHttpRequest.m
//  MKAPI
//
//  Created by 苏尚进 on 2022/11/5.
//  Copyright © 2022 moon. All rights reserved.
//

#import "NSError+MKAPIHttpRequest.h"
#import "MKAPIDictionary.h"
#import "NSObject+MKAPI.h"

NSErrorDomain const MKAPIRequestInvalidateErrorDomain=@"MKAPIRequestInvalidateErrorDomain";
NSErrorUserInfoKey const MKAPIRequestErrorUrlKey=@"url";
NSErrorUserInfoKey const MKAPIRequestErrorServiceCodeKey=@"code";
NSErrorUserInfoKey const MKAPIRequestErrorServiceDescKey=@"desc";
NSString *const MKAPIRequestErrorServiceCodeCertValidFail=@"Cert_Valid_Fail";
@implementation NSError(MKAPIHttpRequest)
+ (NSError *)mkapi_errorWithUrl:(NSString *)url code:(NSInteger)code serviceCode:(nullable NSString *)serviceCode serviceDesc:(nullable NSString *)serviceDesc userInfo:(nullable NSDictionary *)userInfo{
    NSDictionary *errorInfo = MKAPIDictionary
        .PS(userInfo)
        .P(MKAPIRequestErrorUrlKey,url)
        .P(MKAPIRequestErrorServiceCodeKey,serviceCode)
        .P(MKAPIRequestErrorServiceDescKey,serviceDesc)
        .dictionary;
    NSError *error = [NSError errorWithDomain:[NSURL URLWithString:url].host?:@"" code:code userInfo:errorInfo];
    return error;
}
+ (NSError *)mkapi_invalidateErrorWithServiceCode:(nullable NSString *)serviceCode serviceDesc:(nullable NSString *)serviceDesc userInfo:(nullable NSDictionary *)userInfo{
    NSDictionary *errorInfo = MKAPIDictionary
        .PS(userInfo)
        .P(MKAPIRequestErrorServiceCodeKey,serviceCode)
        .P(MKAPIRequestErrorServiceDescKey,serviceDesc)
        .dictionary;
    NSError *error = [NSError errorWithDomain:MKAPIRequestInvalidateErrorDomain code:MKAPIRequestErrorInvalidateError userInfo:errorInfo];
    return error;
}
- (NSString *)mkapi_url{
    return [self.userInfo objectForKey:MKAPIRequestErrorUrlKey];
}
- (NSString *)mkapi_serviceCode{
    return [self.userInfo objectForKey:MKAPIRequestErrorServiceCodeKey];
}
- (NSString *)mkapi_serviceDesc{
    return [self.userInfo objectForKey:MKAPIRequestErrorServiceDescKey];
}
- (NSString *)mkapi_displayCode{
    NSString *result = self.mkapi_serviceCode;
    if(!result){
        result = [@(self.code) stringValue];
    }
    return result;
}
- (NSString *)mkapi_displayDesc{
    NSString *result = self.mkapi_serviceDesc;
    NSDictionary *userInfo = self.userInfo;
    if(!result){
        result = userInfo[NSLocalizedDescriptionKey];
    }
    if(!result){
        result = userInfo[NSLocalizedFailureReasonErrorKey];
    }
    return result;
}
- (BOOL)mkapi_isCertValidError{
    BOOL is = self.code==MKAPIRequestErrorNetworkFrameworkError && [self.mkapi_serviceCode isEqualToString:MKAPIRequestErrorServiceCodeCertValidFail];
    return is;
}
+ (nonnull NSError *)mkapi_certValidErrorForServerTrust:(nullable SecTrustRef)serverTrust url:(nullable NSURL *)url{
    NSDictionary *info = MKAPIDictionary
        .P(NSURLErrorFailingURLPeerTrustErrorKey,(__bridge id _Nullable)(serverTrust))
        .dictionary;
    NSError *error = [NSError mkapi_errorWithUrl:url.absoluteString code:MKAPIRequestErrorNetworkFrameworkError serviceCode:MKAPIRequestErrorServiceCodeCertValidFail serviceDesc:[NSString stringWithFormat:@"%@域名的证书检验失败（%@）。",[NSString mkapi_stringWithSecTrustRef:serverTrust], url.host] userInfo:info];
    return error;
}
@end
